package com.alpaca.recommend.bean;

import com.alpaca.recommend.hbase.HTableClients;
import com.alpaca.recommend.hbase.activerecord.HModel;
import com.alpaca.recommend.util.MD5Util;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by blueback on 2015/2/5.
 */
public class UTags extends HModel<UTags> {

    public static final String TABLE_NAME = "tag_user";

    public static final String U_TAGS_FAMILY = "t";

    public static final String U_TAGS_T_RANK = "r";

    public static final String U_TAGS_T_COUNT = "c";

    public static String getRowKey(String channelCode,String userId,String tag){
        return MD5Util.md5(channelCode)+MD5Util.md5(userId)+tag;
    }

    public void init(String channelCode,String userId,String tag,int rank,int count){
        String rowkey = MD5Util.md5(channelCode)+MD5Util.md5(userId)+tag;
        //设置rowkey.
        setRow(rowkey);
        set(U_TAGS_FAMILY, U_TAGS_T_RANK, rank);
        set(U_TAGS_FAMILY, U_TAGS_T_COUNT, count);
    }


    public static Put getPut(String channelCode, String userId, String TAG, int rank, int count) throws UnsupportedEncodingException {
        String rowKey = MD5Util.md5(String.valueOf(channelCode)) + MD5Util.md5(String.valueOf(userId)) + TAG;
        Put put = new Put(Bytes.toBytes(rowKey));
        put.add(Bytes.toBytes(U_TAGS_FAMILY), Bytes.toBytes(U_TAGS_T_RANK), Bytes.toBytes(rank));
        put.add(Bytes.toBytes(U_TAGS_FAMILY), Bytes.toBytes(U_TAGS_T_COUNT), Bytes.toBytes(count));
        return put;
    }

    public static void main(String[] args) throws IOException {
//        Put put = UTags.getPut("11111","1111","很好",1,1);
        Table table = new HTableClients().getHTable("items");
        Put put = new Put(Bytes.toBytes("items"));
        put.add("i".getBytes(), "t".getBytes(), "10".getBytes());
        table.put(put);
        table.close();
    }
}

